<?php


namespace App\Helpers;


/**
 * 文件助手类
 *
 * Class FileHelper
 *
 * @package App\Helpers
 * @author  kingsg
 */
class FileHelper extends Helper
{
    /**
     * 文件样式：用于预览压缩等处理，需要在cdn配置
     *
     * 常量前缀 file_style
     */
    const FILE_STYLE_LIST = '?imageMogr2/auto-orient/thumbnail/560x560>/blur/1x0';    // 列表(列表图片需要等比压缩展示)

    const APP_OSS_TYPE_QI_NIU        = 'qiniu';
    const APP_OSS_TYPE_TENCENT_CLOUD = 'tencent_cloud';


    // cdn: 前缀
    const CDN_PREFIX_AVATAR  = 1; // 用户头像
    const CDN_PREFIX_SITE_AD = 2; // 地块广告
    const CDN_PREFIX_KEYWORD = [
        self::CDN_PREFIX_AVATAR  => 'avatar',
        self::CDN_PREFIX_SITE_AD => 'siteAD',
    ];

    /**
     * 获取CDN地址host
     */
    public static function getCdnHost()
    {
        switch (env('APP_OSS_TYPE')) {
            case self::APP_OSS_TYPE_QI_NIU:
                return env('QI_NIU_DOMAIN', 'host');
            case self::APP_OSS_TYPE_TENCENT_CLOUD:
                return env('TENCENT_CLOUD_DOMAIN', 'host');
            default:
                return 'host';
        }
    }

    /**
     * 获取完整链接
     *
     * @param string $url
     * @return string
     */
    public static function getCompleteUrl(string $url): string
    {
        if (!$url) {
            return $url;
        }
        // 兼容字符串第一个元素带 '/' 的场景
        if (substr($url, 0, 1) != DIRECTORY_SEPARATOR) {
            $url = DIRECTORY_SEPARATOR . $url;
        }

        return static::getCdnHost() . $url;
    }

    /**
     * 获取完整头像地址
     *
     * @param string $url
     * @return array|string|string[]
     */
    public static function getCompleteAvtar(string $url)
    {
        if (!$url) {
            return $url;
        }

        if (strpos($url, 'http://') || strpos($url, 'https://')) {
            return $url;
        }

        if (strpos($url, 'cos://') === false) {

            $parse_url = parse_url($url);
            if (empty($parse_url['host'])) {
                $url = static::getCompleteUrl($parse_url['path']);
            }

            return $url;
        }

        return str_replace('cos://', static::getCdnHost() . '/public/avatar/', $url);
    }

    /**
     * 加工cdn前缀
     *
     * @param int    $userId
     * @param string $prefix
     * @return string
     */
    public static function makeCdnPrefix(int $userId, string $prefix): string
    {
        if ($userId == 0) {
            $userFlag    = 'sys';    // 一级目录
            $userTwoFlag = 'sys';    // 二级目录
        } else {
            $userFlag    = ($userId % 100);     // 一级取模目录
            $userTwoFlag = ($userId % 10);    // 二级取模目录
        }
        return config('library.tencent_cloud.path_prefix') .
            DIRECTORY_SEPARATOR . $userFlag .
            DIRECTORY_SEPARATOR . $userTwoFlag .
            DIRECTORY_SEPARATOR . $userId .
            DIRECTORY_SEPARATOR . (self::CDN_PREFIX_KEYWORD[$prefix] ?? 'default') .
            DIRECTORY_SEPARATOR . formatDate("Y/m/d");
    }

    /**
     * 获取文件的路径
     *
     * @param string $url
     *
     * @return string
     */
    public static function getFileUrl(string $url): string
    {
        $urlInfo = parse_url($url);
        $fileUrl = $urlInfo['path'];
        if (($urlInfo['query'] ?? '')) {
            $fileUrl .= '?' . $urlInfo['query'];
        }

        return ltrim($fileUrl, DIRECTORY_SEPARATOR);
    }
}
